#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include "ni488.h"

#define ARRAYSIZE 1024                        // Size of read buffer
char ReadBuffer[ARRAYSIZE + 1]; // Device handle
char ErrorMnemonic[21][5] = { "EDVR", "ECIC", "ENOL", "EADR", "EARG", "ESAC",
		"EABO", "ENEB", "EDMA", "", "EOIP", "ECAP", "EFSO", "", "EBUS", "ESTB",
		"ESRQ", "", "", "", "ETAB" };
void GPIBCleanup(int Bd, char* ErrorMsg);

int _cdecl main(void)
{
	int xhy=ibdev(0,5,0,T10s,1,0); //打开信号源
	int sbq=ibdev(1,12,0,T10s,1,0); //打开示波器

	char Str1[]="APPL:SIN ";
	char Str2[]=",5";
	char Str_temp[20];
	char Str_i[7]=" ";

	ibclr(xhy); //信号源清零
	ibclr(sbq); //示波器清零

	for(int i=1000;i<=100000;i+=1000)
	{
		Str_temp[0]='\0';
		itoa(i,Str_i,10);
		strcat(Str_temp,Str1);
		strcat(Str_temp,Str_i);
		strcat(Str_temp,Str2);

		ibwrt(xhy,Str_temp,20L); //设置信号源参数
		ibwrt(sbq,":MEASURE:Vpp:?",14L); //设置示波器参数

		ibrd(sbq,ReadBuffer,ARRAYSIZE);
		ReadBuffer[ibcnt]='\0';
		printf("Data Read: %s\n",ReadBuffer);
	}

	ibonl(xhy,0); //器件挂起
	ibonl(sbq,0);
	return 0;
}

void GPIBCleanup(int ud, char* ErrorMsg) {
	printf("Error : %s\nibsta = 0x%x iberr = %d (%s)\n", ErrorMsg, ibsta,
			iberr, ErrorMnemonic[iberr]);
	if (ud != -1) {
		printf("Cleanup: Taking device offline\n");
		ibonl(ud, 0);
	}
}
